package main;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.GridBagLayout;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JCheckBox;
import javax.swing.JOptionPane;

/**
 * @author kf6kjg
 * 
 */
public class DisplayFrame extends JFrame {
  private static final long serialVersionUID = 1L;
  
  private JMenuBar          jJMenuBar        = null;
  
  private JMenu             jMenu            = null;
  
  private JMenu             jMenu1           = null;
  
  private JMenu             jMenu2           = null;
  
  private JMenu             AlgorithmMenu    = null;
  
  private JPanel            jPanel           = null;
  
  private JMenuItem         jMenuItem        = null;
  
  private JMenuItem         jMenuItem1       = null;
  
  private JMenuItem         jMenuItem11      = null;
  
  private JMenuItem         jMenuItem12      = null;
  
  private JCheckBox         jCheckBox        = null;
  
  private JMenu             jMenu3           = null;
  
  private JMenuItem         jMenuItem2       = null;
  
  private JMenuItem         jMenuItem21      = null;
  
  private JMenuItem         jMenuItem22      = null;
  
  /**
   * This method initializes
   * 
   */
  public DisplayFrame() {
    super();
    initialize();
  }
  
  /**
   * This method initializes this
   * 
   */
  private void initialize() {
    this.setSize(GlobalSettings.DefaultWindowSize);
    this.setContentPane(getJPanel());
    this.setJMenuBar(getJJMenuBar());
    this.setTitle(GlobalSettings.WindowTitle);
    
  }
  
  /**
   * This method initializes jJMenuBar
   * 
   * @return javax.swing.JMenuBar
   */
  private JMenuBar getJJMenuBar() {
    if (jJMenuBar == null) {
      jJMenuBar = new JMenuBar();
      jJMenuBar.add(getJMenu());
      jJMenuBar.add(getJMenu1());
      jJMenuBar.add(getJMenu3());
    }
    return jJMenuBar;
  }
  
  /**
   * This method initializes jMenu
   * 
   * @return javax.swing.JMenu
   */
  private JMenu getJMenu() {
    if (jMenu == null) {
      jMenu = new JMenu();
      jMenu.setText("File");
      jMenu.add(getJMenuItem());
    }
    return jMenu;
  }
  
  /**
   * This method initializes jMenu1
   * 
   * @return javax.swing.JMenu
   */
  private JMenu getJMenu1() {
    if (jMenu1 == null) {
      jMenu1 = new JMenu();
      jMenu1.setText("Settings");
      jMenu1.add(getJMenu2());
      jMenu1.add(getAlgorithmMenu());
      jMenu1.add(getJCheckBox());
    }
    return jMenu1;
  }
  
  /**
   * This method initializes jMenu2
   * 
   * @return javax.swing.JMenu
   */
  private JMenu getJMenu2() {
    if (jMenu2 == null) {
      jMenu2 = new JMenu();
      jMenu2.setText("Speed");
      jMenu2.add(getJMenuItem1());
      jMenu2.add(getJMenuItem11());
      jMenu2.add(getJMenuItem12());
    }
    return jMenu2;
  }
  
  /**
   * This method initializes AlgorithmMenu
   * 
   * @return javax.swing.JMenu
   */
  public JMenu getAlgorithmMenu() {
    if (AlgorithmMenu == null) {
      AlgorithmMenu = new JMenu();
      AlgorithmMenu.setText("Algorithm");
    }
    return AlgorithmMenu;
  }
  
  /**
   * This method initializes jPanel
   * 
   * @return javax.swing.JPanel
   */
  private JPanel getJPanel() {
    if (jPanel == null) {
      jPanel = new JPanel();
      jPanel.setLayout(new GridBagLayout());
    }
    return jPanel;
  }
  
  /**
   * This method initializes jMenuItem
   * 
   * @return javax.swing.JMenuItem
   */
  private JMenuItem getJMenuItem() {
    if (jMenuItem == null) {
      jMenuItem = new JMenuItem();
      jMenuItem.setText("Exit (ESC)");
      jMenuItem.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent e) {
          System.exit(0); // TODO: Make this notify an upstream program listener that exit has been called.
        }
      });
    }
    return jMenuItem;
  }
  
  /**
   * This method initializes jMenuItem1
   * 
   * @return javax.swing.JMenuItem
   */
  private JMenuItem getJMenuItem1() {
    if (jMenuItem1 == null) {
      jMenuItem1 = new JMenuItem();
      jMenuItem1.setText("Fastest (CTRL-BKSP)");
      jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent e) {
          GlobalSettings.FrameDelay = 1l;
        }
      });
    }
    return jMenuItem1;
  }
  
  /**
   * This method initializes jMenuItem11
   * 
   * @return javax.swing.JMenuItem
   */
  private JMenuItem getJMenuItem11() {
    if (jMenuItem11 == null) {
      jMenuItem11 = new JMenuItem();
      jMenuItem11.setText("Faster (CTRL +)");
      jMenuItem11.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent e) {
          if (GlobalSettings.FrameDelay - GlobalSettings.FrameDelayIncr > 0l) {
            GlobalSettings.FrameDelay -= GlobalSettings.FrameDelayIncr;
          }
        }
      });
    }
    return jMenuItem11;
  }
  
  /**
   * This method initializes jMenuItem12
   * 
   * @return javax.swing.JMenuItem
   */
  private JMenuItem getJMenuItem12() {
    if (jMenuItem12 == null) {
      jMenuItem12 = new JMenuItem();
      jMenuItem12.setText("Slower (CTRL -)");
      jMenuItem12.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent e) {
          GlobalSettings.FrameDelay += GlobalSettings.FrameDelayIncr;
        }
      });
    }
    return jMenuItem12;
  }
  
  /**
   * This method initializes jCheckBox
   * 
   * @return javax.swing.JCheckBox
   */
  private JCheckBox getJCheckBox() {
    if (jCheckBox == null) {
      jCheckBox = new JCheckBox();
      jCheckBox.setText("Paused");
      // jCheckBox.setText("Paused (SPACEBAR)");
      jCheckBox.setSelected(SortDemo.getPauseStatus()); // HACK: No modularity... What would happen if SortDemo wasn't the controlling class?
      jCheckBox.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent e) {
          // TODO: Use a event listener to this DisplayFrame...
          
          // HACK
          SortDemo.setPauseStatus(((JCheckBox) e.getSource()).isSelected());
        }
      });
    }
    return jCheckBox;
  }
  
  /**
   * This method initializes jMenu3
   * 
   * @return javax.swing.JMenu
   */
  private JMenu getJMenu3() {
    if (jMenu3 == null) {
      jMenu3 = new JMenu();
      jMenu3.setText("Generate");
      jMenu3.add(getJMenuItem2());
      jMenu3.add(getJMenuItem21());
      jMenu3.add(getJMenuItem22());
    }
    return jMenu3;
  }
  
  /**
   * This method initializes jMenuItem2
   * 
   * @return javax.swing.JMenuItem
   */
  private JMenuItem getJMenuItem2() {
    if (jMenuItem2 == null) {
      jMenuItem2 = new JMenuItem();
      jMenuItem2.setText("Random");
    }
    return jMenuItem2;
  }
  
  /**
   * This method initializes jMenuItem21
   * 
   * @return javax.swing.JMenuItem
   */
  private JMenuItem getJMenuItem21() {
    if (jMenuItem21 == null) {
      jMenuItem21 = new JMenuItem();
      jMenuItem21.setText("Ascending");
      jMenuItem21.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent e) {
          try {
            int value = Integer.parseInt(JOptionPane.showInputDialog("What percentage of randomness? [0-100]", 0));
            if (value < 0) {
              value = 0;
            }
            else if (value > 100) {
              value = 100;
            }
            SortDemo.generateAscending(value * GlobalSettings.VertDetail / 100);
          }
          catch (Exception err) {
            JOptionPane.showMessageDialog(null, "The input you entered was not a number!  Please try again.", "Input Error", JOptionPane.ERROR_MESSAGE);
          }
        }
      });
    }
    return jMenuItem21;
  }
  
  /**
   * This method initializes jMenuItem22
   * 
   * @return javax.swing.JMenuItem
   */
  private JMenuItem getJMenuItem22() {
    if (jMenuItem22 == null) {
      jMenuItem22 = new JMenuItem();
      jMenuItem22.setText("Descending");
      jMenuItem22.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent e) {
          try {
            int value = Integer.parseInt(JOptionPane.showInputDialog("What percentage of randomness? [0-100]", 0));
            if (value < 0) {
              value = 0;
            }
            else if (value > 100) {
              value = 100;
            }
            SortDemo.generateDescending(value * GlobalSettings.VertDetail / 100);
          }
          catch (Exception err) {
            JOptionPane.showMessageDialog(null, "The input you entered was not a number!  Please try again.", "Input Error", JOptionPane.ERROR_MESSAGE);
          }
        }
      });
    }
    return jMenuItem22;
  }
  
} // @jve:decl-index=0:visual-constraint="17,10"
